package util.parser;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import util.parser.html.objects.Anchor;

public class ParseHTML {

	public Document doc = null;

	public String source = null;

	public ParseHTML(File input) {

		
		if(input==null) return;
		String path = input.getPath();

		
		
		if (!input.exists()) {

			return;
		}

	
			if(path==null) return;
			
		try {
		//	System.out.println(input +"\t");
			doc = Jsoup.parse(input, "UTF-8", "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e){
			System.out.println("----Failed at "+input +"\t" );
			return;
		}

		if(doc==null )return;
		/*int index = path.indexOf("www.");

		if (index <= 0) {
			index = path.indexOf("http:/");
			source = "http://" + path.substring(index + 6, path.length());
			// System.out.println(path);
			// System.out.println(source);
			// System.exit(0);
			return;
		}

		source = path.substring(path.indexOf("www."), path.length());
		if (index > 0) {
			source = path.substring(index, path.length());
			index = source.indexOf("www.", 2);
			if (index > 0)
				source = source.substring(index, source.length());

		}
*/
	}

	public String getBodyText() {
		if (doc == null)
			return "";
		if (doc.body() == null)
			return "";

		String text = doc.body().text(); // "An example link"
		return text;

	}
	
	
	public String getDescription() {
		if(doc!=null && doc.select("meta[name=description]")!=null && doc.select("meta[name=description]").first()!=null){
		String desc = doc.select("meta[name=description]").first().attr("content");
		return desc;
		}
		return "";
	}

public String getKeywords() {
		if( doc!=null &&doc.select("meta[name=keywords]")!=null && doc.select("meta[name=keywords]").first()!=null){
			String desc = doc.select("meta[name=keywords]").first().attr("content");
			return desc;
	
		}
		return "";				
		
	}

	
	
	public String getTitle() {
		if( doc!=null && doc.select("meta[name=title]")!=null && doc.select("meta[name=title]").first()!=null){
			
			String title = doc.select("meta[name=title]").first().attr("content");
			return title;
		}
		return "";
	}

	public LinkedList<Anchor> getAnchor() {

		LinkedList<Anchor> l = new LinkedList<Anchor>();

		if (doc == null)
			return null;
		URL baseUrl = null;
		Elements links = doc.select("a");
		Iterator<Element> iter = links.iterator();
		while (iter.hasNext()) {
			Element el = iter.next();
			Anchor a = new Anchor();
			String anchor = el.text().trim();
			String target = el.attr("href");

			if (target.contains("@")
					|| target.toLowerCase().contains("javascript")
					|| target.contains("mailto"))
				continue;

			if (!target.startsWith("www.") && !target.startsWith("http")) {

				try {
					String temporal = source;
					if (!source.contains("http")) {
						temporal = "http://" + source;

					}
					baseUrl = new URL(temporal);

					URL url = new URL(baseUrl, target);

					target = url.toString().replace("http://", "");

				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					continue;
					// System.out.println(baseUrl+ "\t" + target);
					// // e.printStackTrace();
					// System.exit(0);
				}

			}

			if (anchor.equals("")) {

				continue;
			}

			if (target.trim().equals("")) {

				target = source;
			}
			a.setTarget(target.replace("http://", ""));
			a.setSource(source);
			a.setAnchor(anchor);
			l.add(a);
		}

		return l;
	}

	public static void main(String args[]) throws IOException {

		String path = "/home/sergio/data/crawler/hospital/http:/www.cliniclowns.nl/www.cliniclowns.nl/index.html";
		File input = new File(path);

		ParseHTML parser = new ParseHTML(input);

		System.out.println(parser.getBodyText());

		/*
		 * Document doc = Jsoup.parse(input, "UTF-8", ""); Elements links =
		 * doc.select("a"); Iterator<Element> iter = links.iterator();
		 * 
		 * while (iter.hasNext()) {
		 * 
		 * Element el = iter.next(); System.out.println(el.attr("href"));
		 * System.out.println(el.text()); }
		 * 
		 * URL baseUrl = new URL("http://" + "www.cliniclowns.nl"); URL url =
		 * new URL(baseUrl, "index.php"); System.out.println(url);
		 * 
		 * 
		 * String source = "www.clisadfa.html";
		 * System.out.println(source.matches("(.)*\\.[a-z]{3,4}$"));
		 */

	}

}
